package khmt.lvtn.dhbk.summary;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class WriteXSLFile {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		String fileName = "Bao_Cao/Bao_cao2.xlsx";

		// Open and Modify File Excel
		XSSFWorkbook wb = null;
		FileInputStream ins = null;
		try {
			ins = new FileInputStream(fileName);
			wb = new XSSFWorkbook(ins);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		CreationHelper helper = wb.getCreationHelper();
		
		Sheet sheetResult = wb.getSheet("Result");

		CellStyle cellStype = wb.createCellStyle();
		cellStype.setWrapText(true);
		Font font = wb.createFont();
		font.setFontName("Times New Roman");
		font.setBoldweight(Font.BOLDWEIGHT_BOLD);
		font.setColor(IndexedColors.VIOLET.getIndex());
		cellStype.setFont(font);
		cellStype.setAlignment(CellStyle.VERTICAL_BOTTOM);
		cellStype.setBorderTop(CellStyle.BORDER_THIN);
		cellStype.setBorderRight(CellStyle.BORDER_THIN);
		cellStype.setBorderLeft(CellStyle.BORDER_THIN);
		cellStype.setBorderBottom(CellStyle.BORDER_THIN);

		// method get comment in file data write to xlsx
		String oneEntity = "input/comments.txt";
		String plain = "input/plain.txt";
		String result = "positiveOrNegativeResult/resultForAllComments.txt";

		FileReader fr = new FileReader(oneEntity);
		BufferedReader br = new BufferedReader(fr);
		String line = null;

		FileReader frPlain = new FileReader(plain);
		BufferedReader brPlain = new BufferedReader(frPlain);
		String linePlain = null;

		FileReader frResult = new FileReader(result);
		BufferedReader brResult = new BufferedReader(frResult);
		String lineResult = null;

		int i = 1;
		while ((line = br.readLine()) != null
				&& (linePlain = brPlain.readLine()) != null) {
			lineResult = brResult.readLine();
			if (linePlain.equals("p")) {
				linePlain = "positive";
			} else if (linePlain.equals("n")) {
				linePlain = "negative";
			} else {
				linePlain = "neutral";
			}

			if (lineResult.equals("p")) {
				lineResult = "positive";
			} else if (lineResult.equals("n")) {
				lineResult = "negative";
			} else {
				lineResult = "neutral";
			}

			Row row = sheetResult.createRow(i);
			Cell numCell = row.createCell(0);
			Cell commentCell = row.createCell(1);
			Cell userJud = row.createCell(2);
			Cell sysJud = row.createCell(3);
			Cell wrong = row.createCell(4);

			wrong.setCellStyle(cellStype);
			if (!linePlain.equals(lineResult)) {
				wrong.setCellValue("X");
			}

			sysJud.setCellValue(lineResult);
			sysJud.setCellStyle(cellStype);

			userJud.setCellValue(linePlain);
			userJud.setCellStyle(cellStype);

			numCell.setCellValue(i);
			numCell.setCellStyle(cellStype);

			commentCell.setCellValue(helper.createRichTextString(line));
			commentCell.setCellStyle(cellStype);
			i++;
		}
		fr.close();
		frPlain.close();
		frResult.close();
		FileOutputStream fileOut = new FileOutputStream(fileName);
		wb.write(fileOut);
		fileOut.close();

	}

}
